/**
 * 
 */
package com.scpii.universal.hibernate.dao.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.dto.AppPageDto;
import com.scpii.universal.dto.PageTree;
import com.scpii.universal.hibernate.dao.AppPageDao;
import com.scpii.universal.hibernate.entity.AppPageTab;

/**
 * genericWeb
 * @author weny
 * 2012-7-6上午10:32:10
 */
@Repository
public class AppPageDaoImpl  extends BaseDaoImpl<AppPageTab, Integer> implements AppPageDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<AppPageTab> getNextLevelChildren(Integer id, String[] properties) {
		String hql = generateHqlByProperties(properties)
				+ "where entity.intDeleteSign=? and entity.intParentResourceId=? order by entity.intOrder asc";
		return (List<AppPageTab>) getHibernateTemplate().getSessionFactory()
				.getCurrentSession().createQuery(hql).setParameter(0,
						GenericConstants.DELETE_SIGN_EXIST).setParameter(1, id).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AppPageTab> getAllPagesByAppId(int appId, String[] properties) {
		String hql = generateHqlByProperties(properties)
			+ "where entity.intDeleteSign=? and entity.intAppId=?";
		return (List<AppPageTab>) getHibernateTemplate().getSessionFactory()
			.getCurrentSession().createQuery(hql).setParameter(0,
					GenericConstants.DELETE_SIGN_EXIST).setParameter(1, appId).list();
	}
	
	@Override
	public void delete(Integer id) {
		String hql = "update AppPageTab a set a.intDeleteSign=?,a.datUpdateDatetime=? " +
				"where a.intResourceId=?";
		getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createQuery(hql).setParameter(0,
						GenericConstants.DELETE_SIGN_DELETED).setParameter(1,
						new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
								.format(new Date())).setParameter(2, id).executeUpdate();
	}

	@Override
	public void delete(String ids) {
		String sql = "update APP_PAGE_TAB a set a.INT_DELETE_SIGN=?," +
				"a.DAT_UPDATE_DATETIME=? where a.INT_RESOURCE_ID in" + ids;
		getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createSQLQuery(sql).setParameter(0,
						GenericConstants.DELETE_SIGN_DELETED).setParameter(1,
						new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
								.format(new Date())).executeUpdate();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AppPageTab> getPagesInfoByViewId(int viewId, String[] properties) {
		String hql = generateHqlByProperties(properties)
				+ ",AppViewCatelogRalationTab as relation "
				+ "where entity.intResourceId=relation.intResourceId "
				+ "and relation.intDeleteSign=? and relation.intResourceId!=? "
				+ "and relation.intViewId=?";
		return (List<AppPageTab>) getHibernateTemplate().getSessionFactory()
				.getCurrentSession().createQuery(hql).setParameter(0,
						GenericConstants.DELETE_SIGN_EXIST).setParameter(1, 0)
				.setParameter(2, viewId).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AppPageDto> getPagesByNavigation(int appId) {
		String hql = "select new com.scpii.universal.dto.AppPageDto(a.intResourceId,a.varTitleName,navigation.intNavigateType) from AppPageTab a"
				+ ",PageNavigationTab as navigation where a.intResourceId=navigation.intResourceId "
				+ "and navigation.intDeleteSign=? and navigation.intAppId=? "
				+ "order by navigation.intNavigateOrder";
		return (List<AppPageDto>) getHibernateTemplate().getSessionFactory()
				.getCurrentSession().createQuery(hql).setParameter(0,
						GenericConstants.DELETE_SIGN_EXIST).setParameter(1,
						appId).list();
	}

	@Override
	public void updatePage(AppPageTab page) {
		String hql = "update AppPageTab a set a.varTitleName=?,a.intTitleIconId=? where a.intResourceId=?";
		getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createQuery(hql).setParameter(0, page.getVarTitleName())
				.setParameter(1, page.getIntTitleIconId()).setParameter(2,
						page.getIntResourceId()).executeUpdate();
	}

	@Override
	public AppPageTab getPageInfoById(int pageId, String[] properties) {
		String hql = generateHqlByProperties(properties)+ "where entity.intResourceId=?";
		return (AppPageTab) getHibernateTemplate().getSessionFactory()
				.getCurrentSession().createQuery(hql).setParameter(0, pageId)
				.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PageTree> getPageTreeByPageId(int pageId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select {page.*} from page_tree_view page");
		sql.append(" start with page.parent_id=?");
		sql.append(" connect by prior page.page_id=page.parent_id");
		sql.append(" order by page.page_id");
		return getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createSQLQuery(sql.toString()).addEntity("page",
						PageTree.class).setParameter(0, pageId).list();
	}
	
	

}
